<template>

  <h1 class="title">企业人事管理平台</h1>
  <div class="login">
    <div class="selectLogin">
      <router-link to="/loginE"><el-icon :size="40">
          <Message />
        </el-icon></router-link>
      <router-link to="/loginA"><el-icon :size="40" color="grey">
          <User />
        </el-icon></router-link>
    </div>
    <el-card style="width: 400px">
      <template #header>
        <h3 style="text-align: center;">登录</h3>
      </template>
      <el-form class="demo-form-inline" label-width="auto" style="max-width: 800px">
        <el-form-item label="Email">
          <el-input placeholder="Email" v-model="empReq.empEmail" clearable />
        </el-form-item>
        <el-form-item label="Password">
          <el-input placeholder="Password" type="password" v-model="empReq.password" clearable />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="display: flex;justify-content: space-between;">
          <el-button type="primary" @click="doLogin">登录</el-button>
          <router-link to="/resetPassword"><el-link type="primary">忘记密码></el-link></router-link>
        </div>
      </template>
    </el-card>
  </div>


</template>

<script name='Login' setup lang='ts'>
import { ref } from 'vue';
import type { EmpReqELogin } from '../model';
import { loginE } from '../api';
import { useRouter } from 'vue-router';
import { useEmpStore } from '../store/emp';
import { ElMessage } from 'element-plus';

const router = useRouter()

let empReq = ref<EmpReqELogin>({
  empEmail: '',
  password: ''
})
let { setEmpInfo } = useEmpStore()

function doLogin() {
  // 添加表单验证
  if (!empReq.value.empEmail || !empReq.value.password) {
    ElMessage.error('请输入邮箱和密码')
    return
  }
  console.log(empReq.value);
  loginE(empReq.value).then(res => {
    // 检查返回数据是否为空
    if (res.code == 7000) {
      ElMessage.error('登录失败,请检查邮箱和密码是否正确')
      router.push({ path: '/loginE' })
    } else {
      // 登录成功后的处理
      ElMessage.success('登录成功')
      setEmpInfo(res)
      router.push({ path: '/' })
    }
  }).catch(err => {
    // 处理错误情况
    console.error('登录错误:', err)
    ElMessage.error('登录失败,请稍后重试')
  })
}


</script>

<style scoped>
.title {
  margin-top: 30px;
  text-align: center;
}

.login {
  width: 800px;
  margin: 100px auto;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.selectLogin {
  width: 200px;
  display: flex;
  justify-content: space-between;
  padding-bottom: 10px;
}
</style>